var base
var Points = 0#点数(杂货铺的货币)
var HandlingFee = 0#以旧换新手续费(金币)
var RecoveryFloat = 0#回收旧物倍率
var DrinkCosts = 0#异世界饮料费用(点数)
var healhp = 0#异世界饮料回复量
var shoptime = 0#刷新物品之前的购买次数
var PriceFloat = 0#物品价格倍率
var roundtiem = 0#回合数
var PointsRate = 0#点数获取倍率
var CostReduction = 1#费用倍率
var localversion = 1#本地版本
var localversionnum = 0#本地版本序号
var newversion = 1#更新版本
var newversionnum = 13#更新版本序号
var exist = false
var maxheal
var allitems
var voucher = 1#代金券
var RestTime = 0#周末时间
var HoliTime = 0#假日时间
var initial = 1#点数初始值

signal onGameStart		#开始/继续游戏信号

func _ready():
	pass

func _init():
	call_deferred("Init")

func Init():
	if globalData.infoDs.has("g_ZHPBase"):
		base = globalData.infoDs["g_ZHPBase"]
	if base != null:
		base.connect("onNewGame",self,"NewGame")
		base.connect("onLoadGame",self,"GameCtrl")
		base.connect("onStartGame",self,"StartGame")
		base.connect("onSaveGame",self,"BickGame")
		base.connect("onExitGame",self,"BickGame")
		connect("onGameStart",self,"GameStart")
		EraseUseless()
		Update()

func StartGame():
	ItemChara()
	allitems = rareitemx + rareitemy + additem
	allitems.shuffle()
	maxheal = sys.main.player.maxHp
	if base.setting.get("zhiyuan",true):
		initial = 0.3
	if base.setting.get("zhp",true):
		exist = true
		sys.main.connect("onBattleEnd",self,"BattleEnd")
		sys.main.connect("onCharaDel",self,"CharaDel")	

func NewGame():
	emit_signal("onGameStart","new")
	if base.setting.get("zhp") == true:
		roundtiem = sys.rndRan(5,8)
		PointsRate = 0
		CostReduction = 1
		biandong()
		resetitem()

func GameCtrl():
	emit_signal("onGameStart","load")

func GameStart(x):
	if base.setting.get("zhp") == true:
		if x == "load":
			saveAndLoad("load")
			ItemsaveAndLoad("load")
			PointsaveAndload("load")
			# print("旧读取")
		else:
			PointsaveAndload("load")
			voucher = 1
			# print("新读取")

func BickGame():
	RestTime = 0
	HoliTime = 0
	initial = 1
	exist = false
	if base.setting.get("zhp") == true:
		saveAndLoad("save")
		PointsaveAndload("save")
		ItemsaveAndLoad("save")

func BattleEnd():
	roundtiem -= 1
	if roundtiem <= 0:
		roundtiem = sys.rndRan(5,8)
		shoptime = 0
		biandong()
		resetitem()

func Refresh():
	roundtiem = sys.rndRan(5,8)
	shoptime = 0
	PriceFloat = sys.rndRan(1,30)/10 as float
	saveAndLoad("save")
	resetitem()
		
func CharaDel(cha):
	if exist == false:return
	if cha.isSumm == false and cha.team == 2 and cha.isDeath == true:
		Points += (initial*(1+PointsRate+RestTime+HoliTime))
		PointsaveAndload("save")

func biandong():
	HandlingFee = sys.rndRan(5,20)
	RecoveryFloat = sys.rndRan(1,30)/10 as float
	DrinkCosts = sys.rndRan(1,100)
	healhp = sys.rndRan(1,maxheal)
	PriceFloat = sys.rndRan(1,30)/10 as float
	saveAndLoad("save")

var eventItems = {}#商品内容
var rndchara = []

var aa
var bb
var cc
var dd
var ee
var ff
var rnditemA
var rnditemB
var rnditemC
var rnditemD
var rnditemE
var rnditemF

func resetitem():
	aa = sys.rndRan(10,30)*PriceFloat
	bb = sys.rndRan(30,50)*PriceFloat
	cc = sys.rndRan(50,70)*PriceFloat
	dd = sys.rndRan(70,90)*PriceFloat
	ee = sys.rndRan(90,110)*PriceFloat
	ff = sys.rndRan(110,130)*PriceFloat
	rnditemA = getrareItemX()
	rnditemB = getRndItem()
	rnditemC = getRndItem()
	rnditemD = getRndItem()
	rnditemE = getRndItem()
	rnditemF = getrareItemY()
	eventItems = {
		"equipmentA":{
			"btnText": tr("g_itembuy"),
			"price": aa,
			"conn": "buyItem",
			"type": "item",
			"objId": rnditemA,
			"number": 1,
		},
		"equipmentB":{
			"btnText": tr("g_itembuy"),
			"price": bb,
			"conn": "buyItem",
			"type": "item",
			"objId": rnditemB,
			"number": 1,
		},
		"equipmentC":{
			"btnText": tr("g_itembuy"),
			"price": cc,
			"conn": "buyItem",
			"type": "item",
			"objId": rnditemC,
			"number": 1,
		},
		"equipmentD":{
			"btnText": tr("g_itembuy"),
			"price": dd,
			"conn": "buyItem",
			"type": "item",
			"objId": rnditemD,
			"number": 1,
		},
		"equipmentE":{
			"btnText": tr("g_itembuy"),
			"price": ee,
			"conn": "buyItem",
			"type": "item",
			"objId": rnditemE,
			"number": 1,
		},
		"equipmentF":{
			"btnText": tr("g_itembuy"),
			"price": ff,
			"conn": "buyItem",
			"type": "item",
			"objId": rnditemF,
			"number": 1,
		}
	}
	ItemsaveAndLoad("save")

var rareitemx = [
"i_Hide-ZHP-ronghejiechu","i_Hide-ZHP-FragileGreed",
"i_Hide-ZHP-FragileHeart","i_Hide-ZHP-FragileStrength",
"i_Hide-ZHP-CE"
]#自定1
var rareitemy = [
"i_Hide-ZHP-wuzhongshengyou","i_Hide-ZHP-GatheringSwarm",
"i_Hide-ZHP-UnbreakableGreed","i_Hide-ZHP-UnbreakableHeart",
"i_Hide-ZHP-UnbreakableStrength"
]#自定2

# 添加选池装备
var additem = []
var addchara = []
func ItemChara():
	additem.clear()
	# addchara.clear()
	for i in itemData.rndPoolRsl:#选池装备
		additem.append(i)
	# for j in chaData.rndPoolRsl:#选池1级生物
	# 	addchara.append(j)

#从当前池子中获得随机物品
func getRndItem():
	return sys.rndListItem(additem)

func getrareItemX():
	# var x = rareitemx + additem
	return sys.rndListItem(rareitemx)

func getrareItemY():
	return sys.rndListItem(rareitemy)

func getRndItemEnd():
	return sys.rndListItem(allitems)

func rndchara():
	rndchara.clear()
	for i in chaData.rndPoolRsl:
		rndchara.append(i)
	return sys.main.newChara(sys.rndListItem(rndchara))

func saveAndLoad(s):
	if s =="load":
		base.loadData()
		var c = base.setting.get("Shop",[0,1,2,3,4,5,6,7,8])
		HandlingFee = c[0]
		RecoveryFloat = c[1]
		DrinkCosts = c[2]
		healhp = c[3]
		shoptime = c[4]
		PriceFloat = c[5]
		roundtiem = c[6]
		PointsRate = c[7]
		CostReduction = c[8]
	else:
		base.setting["Shop"] = [HandlingFee,RecoveryFloat,DrinkCosts,healhp,shoptime,PriceFloat,roundtiem,PointsRate,CostReduction]
		base.saveData()

func ItemsaveAndLoad(s):
	if s =="load":
		var c = base.setting.get("eventItems",[0,1,2,3,4,5,6,7,8,9,10,11,12])
		eventItems = c[0]
		aa = c[1]
		bb = c[2]
		cc = c[3]
		dd = c[4]
		ee = c[5]
		ff = c[6]
		rnditemA = c[7]
		rnditemB = c[8]
		rnditemC = c[9]
		rnditemD = c[10]
		rnditemE = c[11]
		rnditemF = c[12]
	else:
		base.setting["eventItems"] = [eventItems,aa,bb,cc,dd,ee,ff,rnditemA,rnditemB,rnditemC,rnditemD,rnditemE,rnditemF]
		base.saveData()

func PointsaveAndload(s):
	if s == "load":
		var c = base.setting.get("Points",[0,1])
		Points = c[0]
		voucher = c[1]
	else:
		base.setting["Points"] = [Points,voucher]
		base.saveData()

func Update():
	var c = base.setting.get("version")
	if c == null or c.size() < 2:
		base.setting["version"] = [localversion,localversionnum]
		base.saveData()
	isUpdate()

func isUpdate():
	var c = base.setting.get("version",[0,1])
	localversion = c[0]
	localversionnum = c[1]
	if localversion < newversion or localversionnum < newversionnum:
		sys.newBaseMsg("",tr("g_shopupdate_1"))
		base.setting["version"] = [newversion,newversionnum]
		base.saveData()
		var s = base.setting.get("Points",[0])
		Points = s[0] + (500*(newversion-localversion)) + (30*(newversionnum-localversionnum))
		PointsaveAndload("save")

func EraseUseless():
	var a =  base.setting.get("update")
	var b =  base.setting.get("ce")
	if a != null:
		base.setting.erase("update")
	if b != null:
		base.setting.erase("ce")
	base.saveData()